$(document).ready(function() {
	composeBootLoader.init();
});
var composeBootLoader = {
	
	init: function(){
		$("#tokenHolder").keypress(function(){
			var strToVal = $(this).text();
			composeBootLoader.getRecipient(strToVal);
			$("#recipientList").toggle();
		});
		$("#strBodyMessage").click(function(){
			$("#recipientList").css({
				'display' : 'none'
			});
		});
	},
	getRecipient: function(keywords){
		uri = 'dashboard/get_recipient';
		data = {
			'keywords' : keywords
		};
		$.ajax({
			type: "POST",
			url: base_url+uri,
			data: data,
			dataType: 'json',
			success: function(result){
				var list = $("#recipientList").html('<ul></ul>').find('ul');
				html = '';
				$.each(result.intUserID, function(k,v){
					if(result.strMiddleName[k]!='')
					{
						var strMiddleName = result.strMiddleName[k]+' ';
					}
					else
					{
						var strMiddleName = '';
					}
					html += '<li class="nameHolderRecipient" data-role="'+result.intUserID[k]+'">'+result.strFirstName[k]+' '+strMiddleName+result.strLastName[k]+' &bull; <span class="userAccType">'+result.userType[k]+'</span></li>';
				});
				list.html(html);
				$('.nameHolderRecipient').click(function(){
					var getResID = $(this).attr('data-role');
					var getName = $(this).text();
					$("#tokenHolder").html('<span class="toHolderUi" readonly>'+getName+'<input type="hidden" id="strTo" value="'+getResID+'" disabled="disabled" readonly /><i class="icon-remove-sign" id="removeRes"></i></span>');
					$('#removeRes').click(function(){
						$("#tokenHolder").html('<input type="hidden" id="strTo" value="" />');
					});
					$('#doSendMsg').click(function(){
						var strTo = $('#strTo').val();
						var strMessage = $('#strBodyMessage').val();
						if(strTo=='')
						{
							$('.msgBoxHolder').html('<div class="box error-box"><span>Please select recipient!</span></div>');
							setTimeout(function(){
								$('.msgBoxHolder').html('');
							}, 3000);
						}
						else if(strMessage=='')
						{
							$('.msgBoxHolder').html('<div class="box error-box"><span>Please type your message!</span></div>');
							setTimeout(function(){
								$('.msgBoxHolder').html('');
							}, 3000);
						}
						else
						{
							$('.loaderHolder').show();
							$('#doSendMsg').attr('disabled', true);
							setTimeout(function(){
								composeBootLoader.sendMessage();
							}, 3000);
						}
					});
					$("#recipientList").hide();
					$("#recipientList").html('');
				});
			}
		});
	},
	sendMessage: function(){
		var strTo = $('#strTo').val();
		var strMessage = $('#strBodyMessage').val();
		uri = 'dashboard/send_message';
		data = {
			'strTo' : strTo,
			'strMessage' : strMessage
		};
		$.ajax({
			type: "POST",
			url: base_url+uri,
			data: data,
			dataType: 'json',
			success: function(result){
				console.log(result.intStatus);
				if(result.intStatus==0)
				{
					$('.msgBoxHolder').html('<div class="box success-box"><span>Your message was successfully send!</span></div>');
					$('#doSendMsg').attr('disabled', false);
				}
				else
				{
					$('.msgBoxHolder').html('<div class="box error-box"><span>Your message was not send!</span></div>');
					$('#doSendMsg').attr('disabled', false);
				}
				$('.loaderHolder').hide();
				setTimeout(function(){
					$('.msgBoxHolder').html('');
				}, 3000);
			}
		});
	}
}